package cn.edu.ujn.multiple.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;

import cn.edu.ujn.multiple.model.User;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		String url = request.getRequestURI();
		System.out.println(url);
		//允许访问登录页面
		if(url.indexOf("/login.action")>=0 || url.indexOf("/forget")>=0 || url.indexOf("/enroll")>=0) {
			return true;
		}
		//已登录未登出可以直接访问主页面
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("USER_SESSION");
		if(user!=null) {
			return true;
		}
		//未登录需先登录
		request.setAttribute("msg", "请先进行登录");
		request.getRequestDispatcher("WEB-INF/view/login.jsp").forward(request, response);
		return false;
	}

}
